<?php
/**
 * 权限相关工具类
 * User: t.young
 * Date: 2017/12/29
 * Time: 14:48
 */

namespace backend\utils;


use backend\service\AdminService;

class AuthUtil
{
    /**
     * 检查是否有访问权限
     * @param $url
     * @return  bool
     */
    public static function can($url) {
        $url = preg_replace('/[\/]?([\?]+[r]+[=]+)+/i', '', $url);
        $url = trim($url,"/");
        //检查是否登录
        $isLogin = !\Yii::$app->user->isGuest;
        if (!$isLogin) {
            return false;
        }
        $key = md5(strtolower($url));
        $role_ids = \Yii::$app->user->getState("roleIds");
        if (empty($role_ids)){
            return false;
        }
        $taskArr = AdminService::getTaskByRoleIds($role_ids);
        if (!in_array($key,$taskArr)) {
            return false;
        }
        return true;
    }
}